package redis

import (
	"context"
	"errors"
	"fmt"
	"wx-uniapp-go/third_party/rediscli"
)

const (
	BoUserInfoDataUidKey      = "user:info:%d"
	BoTagDataKey              = "bo:tag"
	BoMapDataKey              = "bo:map"
	BoUserInfoDataUniqueIdKey = "user:info:%s"
)

// SmsCodeType  用户id，发送手机号，验证码
var SmsCodeType = map[string]string{
	"mobile":    "smsMobile:%d:%s:%s",
	"newMobile": "smsNewMobile:%d:%s:%s",
	"wechat":    "smsWechat:%d:%s:%s",
}

func CheckCodeType(ctx context.Context, key, inputCode, codeType string) (err error) {
	if _, ok := SmsCodeType[codeType]; !ok {
		err = errors.New("发送短信类型不存在，请重新输入")
		return
	}
	code, _ := rediscli.Default().Get(ctx, key)
	if code == "" {
		err = errors.New("验证码失效，请重新发送")
		return
	}
	if inputCode != code {
		err = errors.New("验证码错误，请重新输入")
	}
	return
}

func GetCacheKey(key string, a ...interface{}) string {
	return fmt.Sprintf(key, a...)
}
